package cn.iocoder.an.module.business.controller.admin.faultclassification;

import cn.iocoder.an.module.business.controller.admin.equipmentmanage.vo.ImportRespVO;
import cn.iocoder.an.module.business.controller.admin.maintenanceclassification.vo.MaintenanceClassificationImportExcelVO;
import cn.iocoder.an.module.business.enums.MaintenanceStatusEnum;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.faultclassification.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.faultclassification.FaultClassificationDO;
import cn.iocoder.an.module.business.service.faultclassification.FaultClassificationService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 故障类型")
@RestController
@RequestMapping("/business/fault-classification")
@Validated
public class FaultClassificationController {

    @Resource
    private FaultClassificationService faultClassificationService;

    @PostMapping("/create")
    @Operation(summary = "创建故障类型")
    @PreAuthorize("@ss.hasPermission('business:fault-classification:create')")
    public CommonResult<Long> createFaultClassification(@Valid @RequestBody FaultClassificationSaveReqVO createReqVO) {
        return success(faultClassificationService.createFaultClassification(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新故障类型")
    @PreAuthorize("@ss.hasPermission('business:fault-classification:update')")
    public CommonResult<Boolean> updateFaultClassification(@Valid @RequestBody FaultClassificationSaveReqVO updateReqVO) {
        faultClassificationService.updateFaultClassification(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除故障类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:fault-classification:delete')")
    public CommonResult<Boolean> deleteFaultClassification(@RequestParam("id") Long id) {
        faultClassificationService.deleteFaultClassification(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除故障类型")
                @PreAuthorize("@ss.hasPermission('business:fault-classification:delete')")
    public CommonResult<Boolean> deleteFaultClassificationList(@RequestParam("ids") List<Long> ids) {
        faultClassificationService.deleteFaultClassificationListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得故障类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:fault-classification:query')")
    public CommonResult<FaultClassificationRespVO> getFaultClassification(@RequestParam("id") Long id) {
        FaultClassificationDO faultClassification = faultClassificationService.getFaultClassification(id);
        return success(BeanUtils.toBean(faultClassification, FaultClassificationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得故障类型分页")
    @PreAuthorize("@ss.hasPermission('business:fault-classification:query')")
    public CommonResult<PageResult<FaultClassificationRespVO>> getFaultClassificationPage(@Valid FaultClassificationPageReqVO pageReqVO) {
        PageResult<FaultClassificationDO> pageResult = faultClassificationService.getFaultClassificationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FaultClassificationRespVO.class));
    }


    @GetMapping("/list")
    @Operation(summary = "获得全部故障类型")
    public CommonResult<List<FaultClassificationRespVO>> getFaultClassificationList() {
        List<FaultClassificationDO> allList = faultClassificationService.getAllList();
        return success(BeanUtils.toBean(allList, FaultClassificationRespVO.class));
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出故障类型 Excel")
    @PreAuthorize("@ss.hasPermission('business:fault-classification:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFaultClassificationExcel(@Valid FaultClassificationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FaultClassificationDO> list = faultClassificationService.getFaultClassificationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "故障类型.xls", "数据", FaultClassificationRespVO.class,
                        BeanUtils.toBean(list, FaultClassificationRespVO.class));
    }


    @GetMapping("/get-import-template")
    @Operation(summary = "获得故障类型导入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<FaultClassificationImportExcelVO> list = Arrays.asList(
                FaultClassificationImportExcelVO.builder().name("液压").status(MaintenanceStatusEnum.run.getStatus()).description("液压检查").build()
        );
        // 输出
        ExcelUtils.write(response, "故障类型导入模板.xlsx", "故障类型列表", FaultClassificationImportExcelVO.class, list);
    }

    @PostMapping("/import")
    @Operation(summary = "导入故障类型数据")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    @PreAuthorize("@ss.hasPermission('business:fault-classification:import')")
    public CommonResult<ImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                  @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<FaultClassificationImportExcelVO> list = ExcelUtils.read(file, FaultClassificationImportExcelVO.class);
        return success(faultClassificationService.importExcelData(list, updateSupport));
    }

}